package top.kaoshanji.p2ch09a;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import top.kaoshanji.u1constant.FileConstant;

import java.lang.reflect.Method;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * 代码清单-p2ch09a-295页a：Class类的对象的相等性测试
 * author kaoshanji
 * ^_^
 * create time 2025/2/2 10:40
 */
public class ClassIdentity {

    private static final Logger logger = LogManager.getLogger(ClassIdentity.class);

    public static void main(String[] args) {

        logger.info("...代码清单-p2ch09a-295页a：Class类的对象的相等性测试...");

        ClassIdentity ci = new ClassIdentity();

        try {
            ci.test();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    public void test() throws Exception {

        Path path = Paths.get(FileConstant.LOCAL_ROOT_PATH.concat("p2ch09a295页a"));

        FileSystemClassLoader fscl1 = new FileSystemClassLoader(path);
        FileSystemClassLoader fscl2 = new FileSystemClassLoader(path);

        String className = "top.kaoshanji.p2ch09a.support.Sample";

        Class<?> class1 = fscl1.loadClass(className);
        Object obj1 = class1.newInstance();

        Class<?> class2 = fscl2.loadClass(className);
        Object obj2 = class2.newInstance();

        Method setSampleMethod = class1.getMethod("setSample", java.lang.Object.class);

        setSampleMethod.invoke(obj1, obj2);
    }


}
